package com.ghn.cocknovel.net

import com.ghn.cocknovel.constants.Constants
import com.ghn.cocknovel.ext.getUserToken
import com.kt.NetworkModel.App
import com.kt.NetworkModel.bean.*
import com.kt.NetworkModel.utils.MVUtils
import com.kt.network.bean.BaseResult
import com.kt.network.bean.FontDataNew
import com.kt.network.net.ApiService
import com.kt.network.net.RetrofitClient
import io.reactivex.Observable
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.ResponseBody
import java.io.File


/**
 * @author 浩楠
 *
 * @date 2023/1/9   17:08.
 *
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * 描述:请求管理器
 */
class DataService {


    companion object {
        private val shopId = 159503

        /**
         * 登录接口
         */
        fun login(username: String, password: String): Observable<BaseResult<LoginBean>> {
            val params = HashMap<String, Any>()
            params.put("userAccount", username)
            params.put("userPassword", password)
            params.put("shopId", "159503")
            params.put("platForm", Constants.PlatForm.FORM_ANDROID)
            params.put("token", "61cab21b-984a-40b5-ad89-e1c3f0e55b22")
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).Login(params)
        }

        /**
         *司机信息查询
         */
        fun quickMessage(): Observable<BaseResult<QuickMessageBean>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).quickmessage(params)
        }

        /**
         *司机信息查询
         */
        fun driverUpdateTransitDriverInfo(
            driverName: String,
            driverAvatar: String,
            driverGender: Int,
            driverBirth: String,
            accountName: String,
            driverNumber: String,
            organizationName: String,
            entryTime: String,
            drivingAge: Int,
            provinceId: String,
            provinceName: String,
            cityId: String,
            cityName: String,
            areaId: String,
            areaName: String
        ): Observable<BaseResult<TransitArriveStartingPointBean>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("token", MVUtils.getUserToken())
            params.put("driverName", driverName)
            params.put("driverAvatar", driverAvatar)
            params.put("driverGender", driverGender)
            params.put("driverBirth", driverBirth)
            params.put("accountName", accountName)
            params.put("driverNumber", driverNumber)
            params.put("organizationName", organizationName)
            params.put("entryTime", entryTime)
            params.put("drivingAge", drivingAge)
            params.put("provinceId", provinceId)
            params.put("provinceName", provinceName)
            params.put("cityId", cityId)
            params.put("cityName", cityName)
            params.put("areaId", areaId)
            params.put("areaName", areaName)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).driverUpdateTransitDriverInfo(params)
        }

        /**
         *司机信息查询
         */
        fun getonlinetime(): Observable<BaseResult<OnlineBean>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).getonlinetime(params)
        }

        /**
         * 累计订单数统计
         */
        fun getcumulative(): Observable<BaseResult<CumulativeBean>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).getcumulative(params)
        }

        /**
         * 查询司机当前任务信息
         */
        fun getinformation(): Observable<BaseResult<InformationBean>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).getinformation(params)
        }

        /**
         * 查询上一次任务信息
         */
        fun getaboutinformation(): Observable<BaseResult<InformationBean>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).getaboutinformation(params)
        }

        /**
         * 周月累计时长统计
         */
        fun getcumulativenduartion(): Observable<BaseResult<CumulativenduartionBean>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).getcumulativenduartion(params)
        }

        /**
         * 周/月评分
         */
        fun getscorestatistics(): Observable<BaseResult<ScorestatisticsBean>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).getscorestatistics(params)
        }

        /**
         * 修改密码
         */
        fun getchangepassword(password: String): Observable<BaseResult<ChangePasswordBean>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("password", password)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).getchangepassword(params)
        }

        /**
         * 修改手机号
         */
        fun getchangepathonenumber(userMobile: String): Observable<BaseResult<ChangePasswordBean>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("userMobile", userMobile)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).getchangepathonenumber(params)
        }

        /**
         * 意见反馈
         */
        fun savefeedback(problemDescription: String): Observable<BaseResult<ChangePasswordBean>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("userMobile", problemDescription)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).savefeedback(params)
        }


        /**
         * 订单列表查询
         */
        fun queryDriverTransitOrderList(currentPage: Int, taskId: String): Observable<BaseResult<MutableList<queryDriverTransitOrderListBean>>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("taskId", taskId)
            params.put("currentPage", currentPage)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).queryDriverTransitOrderList(params)
        }


        /**
         * 查询列表详情
         */
        fun queryTransitOrder(orderId: String): Observable<BaseResult<QuerydrivertranstiorBean>> {
            val params = HashMap<String, Any>()
            params.put("orderId", orderId)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).queryTransitOrder(params)
        }

        /**
         * 地址查询
         */
        fun queryAddress(districtParentId: Int = 0): Observable<BaseResult<MutableList<MyAddressBean>>> {
            val params = HashMap<String, Any>()
            params.put("districtParentId", districtParentId)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).queryAddress(params)
        }


        /**
         * 司机查询排班任务
         */
        fun driverQueryTransitSchedulePlanList(currentPage: Int, scheduleDate: String = "", pageSize: Int = 20): Observable<BaseResult<ResultListBean<ToDayWorkTaskBean>>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("token", MVUtils.getUserToken())
            params.put("currentPage", currentPage)
            params.put("pageSize", pageSize)
            params.put("scheduleDate", scheduleDate)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).driverQueryTransitSchedulePlanList(params)
        }

        /**
         * 司机确认开始任务
         */
        fun confirmTransitSchedulePlan(taskId: String, carLongitude: String, carLatitude: String, loginDevice: String): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("platForm", Constants.PlatForm.FORM_ANDROID)
            params.put("token", MVUtils.getUserToken())
            params.put("id", taskId)
            params.put("carLongitude", carLongitude)
            params.put("carLatitude", carLatitude)
            params.put("loginDevice", loginDevice)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).confirmTransitSchedulePlan(params)
        }

        /**
         * 查询司机所有排班日期
         */
        fun querySchedulePlanDateList(): Observable<BaseResult<MutableList<String>>> {
            val params = HashMap<String, Any>()
            params.put("shopId", shopId)
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).querySchedulePlanDateList(params)
        }

        /**
         * 司机确认结束任务
         */
        fun finishTransitSchedulePlanNet(taskId: String, carLongitude: String, carLatitude: String): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("id", taskId)
            params.put("shopId", shopId)
            params.put("carLongitude", carLongitude)
            params.put("carLatitude", carLatitude)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).finishTransitSchedulePlan(params)
        }

        /**
         * 任务详情页查询任务信息
         */
        fun queryPassengerBySite(): Observable<BaseResult<QueryPassengerBySiteBean>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("shopId", shopId)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).queryPassengerBySite(params)
        }

        /**
         * 查询区域坐标集合
         */
        fun queryTransitFenceAreaCoordinate(fenceAreaId: String): Observable<BaseResult<MutableList<QueryTransitFenceAreaCoordinateBean>>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("fenceAreaId", fenceAreaId)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).queryTransitFenceAreaCoordinate(params)
        }

        /**
         * 查询区域坐标集合
         */
        fun squeryPassengerBySite(id: String): Observable<BaseResult<SqueryPassengerBySiteBean>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("id", id)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).squeryPassengerBySite(params)
        }

        /**
         * 司机端订单列表查询
         */
        fun squeryDriverTransitOrderList(siteId: String, operate: Int, taskId: String): Observable<BaseResult<BqueryBean<BQueryDriverTransitOrderListBean>>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("shopId", shopId)
            params.put("platForm", 3)
            params.put("currentPage", 1)
            params.put("pageSize", 10)
            params.put("siteId", siteId)
            params.put("operate", operate)
            params.put("taskId", taskId)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).squeryDriverTransitOrderList(params)
        }

        /**
         * 司机端订单列表查询
         */
        fun AllsqueryDriverTransitOrderList(siteId: String, taskId: String): Observable<BaseResult<BqueryBean<BQueryDriverTransitOrderListBean>>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("shopId", shopId)
            params.put("platForm", 3)
            params.put("currentPage", 1)
            params.put("pageSize", 10)
            params.put("siteId", siteId)
            params.put("taskId", taskId)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).squeryDriverTransitOrderList(params)
        }

        /**
         * 司机到达站点
         */
        fun transitArriveStartingPoint(siteId: Int, carLongitude: String, carLatitude: String): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("shopId", shopId)
            params.put("siteId", siteId)
            params.put("carLongitude", carLongitude)
            params.put("carLatitude", carLatitude)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).transitArriveStartingPoint(params)
        }

        /**
         * 司机确认乘客上车
         */
        fun getOnTransit(orderId: String, siteId: Int, carLongitude: String, carLatitude: String): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("shopId", shopId)
            params.put("orderId", orderId)
            params.put("siteId", siteId)
            params.put("carLongitude", carLongitude)
            params.put("carLatitude", carLatitude)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).getOnTransit(params)
        }

        /**
         * 用户超时未上车司机取消订单
         */
        fun cancelTimeoutOrder(orderId: String): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("shopId", shopId)
            params.put("orderId", orderId)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).cancelTimeoutOrder(params)
        }

        /**
         * 用户超时未上车司机取消订单
         */
        suspend fun uploadFile(path: String, fileType: String = "siteImage"): ResponseBody {
            val lastIndexOf: Int = path.lastIndexOf(".")
            val extName = path.substring(lastIndexOf)
            val fileRQ: RequestBody = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), File(path))
            val body = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "bbb" + extName, fileRQ).addFormDataPart("fileType", fileType).build()
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).uploadFile(body)
        }

        /**
         * 用户超时未上车司机取消订单
         */
        suspend fun driverUpdateTransitDriverInfo(driverAvatar: String): BaseResult<String> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("shopId", shopId)
            params.put("driverAvatar", driverAvatar)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).driverUpdateTransitDriverLogoInfo(params)
        }

        /**
         * 司机端实时记录车辆位置
         */
        fun saveTransitPositionInfo(carLatitude: String, carLongitude: String): Observable<BaseResult<TransitArriveStartingPointBean>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("carLatitude", carLatitude)
            params.put("carLongitude", carLongitude)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).saveTransitPositionInfo(params)
        }

        /**
         *  车辆人数统计
         */
        fun queryTransitSeatStatistics(taskId: String): Observable<BaseResult<QueryTransitSeatStatisticsBean>> {
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).queryTransitSeatStatistics(taskId)
        }

        /**
         *  车辆人数统计
         */
        fun updateDriverNextSite(siteId: String, carLatitude: String, carLongitude: String): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("siteId", siteId)
            params.put("carLatitude", carLatitude)
            params.put("carLongitude", carLongitude)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).updateDriverNextSite(params)
        }

        /**
         *  确认乘客下车
         */
        fun arriveTransit(orderId: String, siteId: String, carLatitude: String, carLongitude: String): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("shopId", shopId)
            params.put("orderId", orderId)
            params.put("siteId", siteId)
            params.put("carLatitude", carLatitude)
            params.put("carLongitude", carLongitude)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).updateDriverNextSite(params)
        }

        /**
         *
         *根据任务id 查询订单量统计
         */
        fun queryDriverOrderStatisticsByTask(taskId: String): Observable<BaseResult<QueryDriverOrderStatisticsByTaskBean>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("shopId", shopId)
            params.put("taskId", taskId)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).queryDriverOrderStatisticsByTask(params)
        }

        /**
         *
         *查询司机提示语【司机点弹窗触发后台关闭弹窗接口 后台设置关闭】【需要定时任务定时查询】
         */
        fun queryTransitPromptNet(): Observable<BaseResult<QueryTransitPromptBean>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).queryTransitPromptNet(params)
        }

        /**
         *
         *司机暂停或重新开始派单
         */
        fun updateDiverReceiveOrderNet(canReceiveOrder: String): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("shopId", shopId)
            params.put("canReceiveOrder", canReceiveOrder)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).updateDiverReceiveOrder(params)
        }

        /**
         *查询司机后续路径站点信息【不包括当前的站点 当前站点还是从查询任务信息里面拿的】
         */
        fun queryTransitRouteNet(): Observable<BaseResult<MutableList<QueryTransitRouteBean>>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).queryTransitRoute(params)
        }

        /**
         *关闭弹窗提示
         */
        fun updateTransitPromptNet(): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).updateTransitPrompt(params)
        }

        /**
         *关闭弹窗提示
         */
        fun cancelTransitSchedulePlanNet(taskId: String): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("id", taskId)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).cancelTransitSchedulePlan(params)
        }

        /**
         *到达站点后获取倒计时
         */
        fun getSiteCountdown(siteId: String): Observable<BaseResult<String>> {
            val params = HashMap<String, Any>()
            params.put("token", MVUtils.getUserToken())
            params.put("siteId", siteId)
            params.put("shopId", shopId)
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java).getSiteCountdown(params)
        }

//*********************************************************************************************************************************************************
        /**
         * 首页接口
         */
        suspend fun callback(host: Int): BaseResult<FontDataNew> {
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java, host).callback()
        }

        /**
         * banner
         */
        suspend fun wbanner(host: Int): BaseResult<MutableList<WBanner.Data>> {
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java, host).banner()
        }

        /**
         * 项目分类
         */
        suspend fun project(host: Int): BaseResult<MutableList<ProjectBean.Data>> {
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java, host).project()
        }

        /**
         * 项目分类的详情
         */
        suspend fun project_content(host: Int, page: Int, cid: Int): BaseResult<TabFrameBean.Data> {
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java, host).project_content(page, cid)
        }

        /**
         * 下载
         */
        suspend fun download(host: Int, url: String): Any? {
            return RetrofitClient.getInstance(App.get()).getDefault(ApiService::class.java, host).downloadFile(url)
        }

    }
}